package org.apache.click.doubleclick.inject;

import org.apache.click.Page;
import org.apache.commons.lang.Validate;

/**
 * Inject aware {@link org.apache.click.Page}. User can inherit this page to inject
 * dependencies.
 * <p/>
 * <b><u>IMPORTANT</u></b>: If dependency injector control (e.g: SpringControl,
 * GuiceControl, etc) is configured on click.xml, Then no need to extend this
 * page. On that case the injection takes place automatically.
 *
 * @author Jahid Shohel(development.jsh@gmail.com)
 */
public class InjectAwarePage extends Page {
    protected InjectAwarePage() {

        Object dependencyInjectorObj = getContext().getServletContext().getAttribute(DependencyInjectorPageInterceptor.DEPENDENCY_INJECTOR);

        Validate.notNull(dependencyInjectorObj, "Dependency injector is null on servlet context");
        Validate.isTrue(dependencyInjectorObj instanceof DependencyInjector, "Invalid dependency injector. Expected " + DependencyInjector.class.getSimpleName() + ", but found " + dependencyInjectorObj.getClass().getSimpleName());

        DependencyInjector dependencyInjector = (DependencyInjector) dependencyInjectorObj;
        dependencyInjector.injectDependencies(this);
    }
}
